package org.web3j.protocol.core.methods.response;

import java.io.IOException;
import java.util.Optional;

import org.web3j.protocol.ObjectMapperFactory;
import org.web3j.protocol.core.Response;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectReader;

public class EthPendingTransaction extends Response<PendingTransaction> {

	public Optional<PendingTransaction> getTransaction() {
		return Optional.ofNullable(getResult());
	}

	public static class ResponseDeserialiser extends JsonDeserializer<PendingTransaction> {

		private ObjectReader objectReader = ObjectMapperFactory.getObjectReader();

		@Override
		public PendingTransaction deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
			if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) {
				return objectReader.readValue(jsonParser, PendingTransaction.class);
			} else {
				return null;
			}
		}
	}
}
